package com.example.idachuappone.index.item;

import java.io.Serializable;
import java.util.HashMap;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.sina.weibo.SinaWeibo.ShareParams;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.index.dialog.IDialog;
import com.example.idachuappone.index.dto.FamilyFeastDto;
import com.example.idachuappone.index.entity.FamilyFeast;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.CheckUtil;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class FamilyFeastDetailActivity extends BaseActivity implements OnClickListener {
	private Button btn_back;
	private Button btn_share;
	private Button btn_submit;
	private BitmapUtils bu;
	private Dialog dialogLoading;
	private Dialog dialogShare;
	private FamilyFeast familyFeast;
	private Handler handler;
	private String id;
	private ImageView img_head;
	private ImageView img_tu1;
	private ImageView img_tu2;
	private PlatformActionListener paListener;
	private TextView tv_all_image;
	private TextView tv_description;
	private TextView tv_img_num;
	private TextView tv_name;
	private TextView tv_recipes;
	private TextView tv_service_flow;
	private TextView tv_slogan;

	public FamilyFeastDetailActivity() {
		this.handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 111 /* 111 */:
					MainToast.show(FamilyFeastDetailActivity.this, "\u5206\u4eab\u5931\u8d25", 0);
				case 11111:
					MainToast.show(FamilyFeastDetailActivity.this, "\u5206\u4eab\u53d6\u6d88", 0);
				default:
				}
			}
		};
		this.paListener = new PlatformActionListener() {
			@Override
			public void onError(Platform platform, int action, Throwable t) {
				FamilyFeastDetailActivity.this.handler.sendEmptyMessage(111);
			}

			@Override
			public void onComplete(Platform arg0, int arg1, HashMap<String, Object> hashMap) {
				FamilyFeastDetailActivity.this.handler.sendEmptyMessage(1111);
			}

			@Override
			public void onCancel(Platform platform, int action) {
				FamilyFeastDetailActivity.this.handler.sendEmptyMessage(11111);
			}
		};
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_family_feast_detail);
		ShareSDK.initSDK(this);
		this.bu = BitmapUtilHelper.createBitmapUtil(this);
		this.dialogLoading = ComUtil.createCusProgressSmallBlackBgDialog(this);
		this.id = getIntent().getStringExtra("id");
		initView();
		checkNet();
	}

	private void checkNet() {
		if (CheckUtil.checkNet(this)) {
			initData();
		} else {
			MainToast.show(this, getResources().getString(R.string.txt_network), 0);
		}
	}

	private void initView() {
		this.btn_share = (Button) findViewById(R.id.btn_share);
		this.btn_share.setOnClickListener(this);
		this.tv_name = (TextView) findViewById(R.id.tv_name);
		this.tv_slogan = (TextView) findViewById(R.id.tv_slogan);
		this.tv_description = (TextView) findViewById(R.id.tv_description);
		this.tv_recipes = (TextView) findViewById(R.id.tv_recipes);
		this.img_head = (ImageView) findViewById(R.id.img_head);
		this.img_tu1 = (ImageView) findViewById(R.id.img_tu1);
		this.img_tu1.setOnClickListener(this);
		this.img_tu2 = (ImageView) findViewById(R.id.img_tu2);
		this.img_tu2.setOnClickListener(this);
		this.tv_img_num = (TextView) findViewById(R.id.tv_img_num);
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.btn_submit = (Button) findViewById(R.id.btn_submit);
		this.btn_submit.setOnClickListener(this);
		this.tv_all_image = (TextView) findViewById(R.id.tv_all_image);
		this.tv_all_image.setOnClickListener(this);
		this.tv_service_flow = (TextView) findViewById(R.id.tv_service_flow);
	}

	private void initData() {
		this.dialogLoading.show();
		NetUtil.get(this, new StringBuilder(Constant.FEATURE_ID).append(this.id).toString(), 100,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						FamilyFeastDetailActivity.this.dialogLoading.dismiss();
						String json = NetUtil.unescapeUnicode(responseInfo.result);
						try {
							FamilyFeastDetailActivity.this.familyFeast = new FamilyFeastDto().getFamilyFeast(
									FamilyFeastDetailActivity.this, json);
							FamilyFeastDetailActivity.this.setViewData();
						} catch (Exception e) {
							MainToast.show(FamilyFeastDetailActivity.this, e.getMessage(), 0);
						}
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						FamilyFeastDetailActivity.this.dialogLoading.dismiss();
						MainToast.show(FamilyFeastDetailActivity.this, FamilyFeastDetailActivity.this.getResources()
								.getString(R.string.netFail), 0);
					}
				});
	}

	private void setViewData() {
		int i;
		this.tv_name.setText(this.familyFeast.getName());
		this.tv_slogan.setText(this.familyFeast.getSlogan());
		this.tv_description.setText(this.familyFeast.getDescription());
		this.bu.display(this.img_head, this.familyFeast.getHead());
		String temp = "";
		if (this.familyFeast.getRecipes() != null) {
			for (i = 0; i < this.familyFeast.getRecipes().size(); i++) {
				temp = new StringBuilder(String.valueOf(temp)).append(this.familyFeast.getRecipes().get(i).getName())
						.append("    ").toString();
			}
		}
		this.tv_recipes.setText(temp);
		if (this.familyFeast.getImgs() != null && this.familyFeast.getImgs().size() >= 2) {
			this.img_tu1.setVisibility(0);
			this.img_tu2.setVisibility(0);
			this.tv_img_num.setText("\u6700\u65b0\u56fe\u7247(" + this.familyFeast.getImgs().size() + ")");
			this.bu.display(this.img_tu1, this.familyFeast.getImgs().get(0).getImg_small());
			this.bu.display(this.img_tu2, this.familyFeast.getImgs().get(1).getImg_small());
		} else if (this.familyFeast.getImgs() == null || this.familyFeast.getImgs().size() != 1) {
			this.img_tu1.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			this.img_tu2.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		} else {
			this.img_tu1.setVisibility(0);
			this.img_tu2.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			this.tv_img_num.setText("\u6700\u65b0\u56fe\u7247(" + this.familyFeast.getImgs().size() + ")");
			this.bu.display(this.img_tu1, this.familyFeast.getImgs().get(0).getImg_small());
		}
		String str = "";
		for (i = 0; i < this.familyFeast.getFlow().size(); i++) {
			str = new StringBuilder(String.valueOf(str)).append(i + 1).append("\u3001")
					.append(this.familyFeast.getFlow().get(i)).toString();
			if (i != this.familyFeast.getFlow().size() - 1) {
				str = new StringBuilder(String.valueOf(str)).append("\n").toString();
			}
		}
		this.tv_service_flow.setText(str);
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.btn_share /* 2131165240 */:
			this.dialogShare = createBottomShareDialog();
			this.dialogShare.show();
		case R.id.img_tu1 /* 2131165250 */:
			if (this.familyFeast != null && this.familyFeast.getImgs() != null && this.familyFeast.getImgs().size() > 0) {
				intent = new Intent(this, FamilyFeastTwoImageVpActivity.class);
				intent.putExtra("list", (Serializable) this.familyFeast.getImgs());
				intent.putExtra("index", 0);
				startActivity(intent);
			}
		case R.id.img_tu2 /* 2131165251 */:
			if (this.familyFeast != null && this.familyFeast.getImgs() != null && this.familyFeast.getImgs().size() > 0) {
				intent = new Intent(this, FamilyFeastTwoImageVpActivity.class);
				intent.putExtra("list", (Serializable) this.familyFeast.getImgs());
				intent.putExtra("index", 1);
				startActivity(intent);
			}
		case R.id.btn_submit /* 2131165282 */:
			new IDialog().showPhone(this).show();
		case R.id.tv_all_image /* 2131165298 */:
			if (this.familyFeast != null && this.familyFeast.getImgs() != null && this.familyFeast.getImgs().size() > 0) {
				Intent allIntent = new Intent(this, FamilyFeastTwoMenuActivity.class);
				allIntent.putExtra("list", (Serializable) this.familyFeast.getImgs());
				startActivity(allIntent);
			}
		case R.id.ll_dismiss /* 2131165441 */:
		case R.id.txt_share_sel_cancel /* 2131165630 */:
			this.dialogShare.dismiss();
		case R.id.lLayout_share_to_weixin /* 2131165622 */:
			this.dialogShare.dismiss();
			if (this.familyFeast != null) {
				ShareParams spweixin = new ShareParams();
				spweixin.setTitle(new StringBuilder(String.valueOf(this.familyFeast.getName())).append(
						"\u5bb6\u5bb4\u670d\u52a1").toString());
				spweixin.setText("\u60f3\u5728\u5bb6\u4eab\u53d7\u9152\u5e97\u4f1a\u6240\u7528\u9910\u5f85\u9047\u4e48\uff1f\u53ea\u9700\u6765\u7231\u5927\u53a8\u8f7b\u677e\u9884\u7ea6\uff0c\u661f\u7ea7\u53a8\u5e08\u3001\u4e13\u4e1a\u670d\u52a1\u5168\u90e8\u7edf\u7edf\u4e0a\u95e8\uff1b");
				spweixin.setImageUrl(this.familyFeast.getImgs().get(0).getImg());
				spweixin.setUrl(new StringBuilder(Constant.FEATURE_SHARE_CONTENT).append(this.familyFeast.getId())
						.toString());
				spweixin.setShareType(BitmapCacheManagementTask.MESSAGE_CLEAR);
				Platform weixin = ShareSDK.getPlatform(Wechat.NAME);
				weixin.setPlatformActionListener(this.paListener);
				weixin.share(spweixin);
			}
		case R.id.lLayout_share_to_wxpy /* 2131165624 */:
			this.dialogShare.dismiss();
			if (this.familyFeast != null) {
				ShareParams spweixinf = new ShareParams();
				spweixinf
						.setTitle("\u60f3\u5728\u5bb6\u4eab\u53d7\u9152\u5e97\u4f1a\u6240\u7528\u9910\u5f85\u9047\u4e48\uff1f\u53ea\u9700\u6765\u7231\u5927\u53a8\u8f7b\u677e\u9884\u7ea6\uff0c\u661f\u7ea7\u53a8\u5e08\u3001\u4e13\u4e1a\u670d\u52a1\u5168\u90e8\u7edf\u7edf\u4e0a\u95e8\uff1b");
				spweixinf.setImageUrl(this.familyFeast.getImgs().get(0).getImg());
				spweixinf.setUrl(new StringBuilder(Constant.FEATURE_SHARE_CONTENT).append(this.familyFeast.getId())
						.toString());
				spweixinf.setShareType(BitmapCacheManagementTask.MESSAGE_CLEAR);
				Platform weixinf = ShareSDK.getPlatform(WechatMoments.NAME);
				weixinf.setPlatformActionListener(this.paListener);
				weixinf.share(spweixinf);
			}
		case R.id.lLayout_share_to_qq /* 2131165626 */:
			this.dialogShare.dismiss();
			if (this.familyFeast != null) {
				ShareParams sp1 = new ShareParams();
				sp1.setTitle(new StringBuilder(String.valueOf(this.familyFeast.getName())).append(
						"\u5bb6\u5bb4\u670d\u52a1").toString());
				sp1.setTitleUrl(new StringBuilder(Constant.FEATURE_SHARE_CONTENT).append(this.familyFeast.getId())
						.toString());
				sp1.setText("\u60f3\u5728\u5bb6\u4eab\u53d7\u9152\u5e97\u4f1a\u6240\u7528\u9910\u5f85\u9047\u4e48\uff1f\u53ea\u9700\u6765\u7231\u5927\u53a8\u8f7b\u677e\u9884\u7ea6\uff0c\u661f\u7ea7\u53a8\u5e08\u3001\u4e13\u4e1a\u670d\u52a1\u5168\u90e8\u7edf\u7edf\u4e0a\u95e8\uff1b");
				sp1.setImageUrl(this.familyFeast.getImgs().get(0).getImg());
				sp1.setSite(getString(R.string.app_name));
				sp1.setSiteUrl(new StringBuilder(Constant.FEATURE_SHARE_CONTENT).append(this.familyFeast.getId())
						.toString());
				Platform qzone = ShareSDK.getPlatform(QZone.NAME);
				qzone.setPlatformActionListener(this.paListener);
				qzone.share(sp1);
			}
		case R.id.lLayout_share_to_sina /* 2131165628 */:
			this.dialogShare.dismiss();
			if (this.familyFeast != null) {
				ShareParams sp = new ShareParams();
				sp.setText("\u60f3\u5728\u5bb6\u4eab\u53d7\u9152\u5e97\u4f1a\u6240\u7528\u9910\u5f85\u9047\u4e48\uff1f\u53ea\u9700\u6765\u7231\u5927\u53a8\u8f7b\u677e\u9884\u7ea6\uff0c\u661f\u7ea7\u53a8\u5e08\u3001\u4e13\u4e1a\u670d\u52a1\u5168\u90e8\u7edf\u7edf\u4e0a\u95e8\uff1bhttp://www.idachu.cn/feature/view/"
						+ this.familyFeast.getId() + "  @\u7231\u5927\u53a8");
				sp.setImageUrl(this.familyFeast.getImgs().get(0).getImg());
				Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
				weibo.setPlatformActionListener(this.paListener);
				weibo.share(sp);
			}
		default:
		}
	}

	private Dialog createBottomShareDialog() {
		View view = LayoutInflater.from(this).inflate(R.layout.view_bottom_dialog_share, null);
		view.setMinimumWidth(getWindowManager().getDefaultDisplay().getWidth());
		view.setMinimumHeight(getWindowManager().getDefaultDisplay().getHeight());
		((LinearLayout) view.findViewById(R.id.ll_anim)).startAnimation(AnimationUtils.loadAnimation(this,
				R.anim.dialog_share_content_in));
		((LinearLayout) view.findViewById(R.id.ll_dismiss)).setOnClickListener(this);
		((LinearLayout) view.findViewById(R.id.lLayout_share_to_qq)).setOnClickListener(this);
		((LinearLayout) view.findViewById(R.id.lLayout_share_to_sina)).setOnClickListener(this);
		((LinearLayout) view.findViewById(R.id.lLayout_share_to_weixin)).setOnClickListener(this);
		((LinearLayout) view.findViewById(R.id.lLayout_share_to_wxpy)).setOnClickListener(this);
		((TextView) view.findViewById(R.id.txt_share_sel_cancel)).setOnClickListener(this);
		Dialog dialog = new Dialog(this, 2131361803);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(false);
		Window dialogWindow = dialog.getWindow();
		LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(83);
		lp.x = 0;
		lp.y = 0;
		dialogWindow.setAttributes(lp);
		return dialog;
	}
}
